package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Role;
import com.atguigu.service.PermissionService;
import com.atguigu.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

    private static final String PAGE_INDEX = "role/index";
    private static final String PAGE_CREATE = "role/create";
    private static final String ACTION_LIST = "redirect:/role";
    private static final String PAGE_EDIT = "role/edit";
    private static final String PAGE_ASSIGN_ROLE = "role/assignShow";
    @Reference
    RoleService roleService;

    @Reference
    PermissionService permissionService;


    @RequestMapping("/assignPermission")
    @PreAuthorize("hasAuthority('role.assgin')")
    public String assignRole(Long roleId,Long[] permissionIds,HttpServletRequest request){
        //注意：操作中间表数据，通过多对多两端任意服务接口都行。中间表不提供业务接口
        permissionService.assignPermission(roleId, permissionIds);

        return this.successPage(null, request);
    }


    @RequestMapping("/assignPermission/{roleId}")
    @PreAuthorize("hasAuthority('role.assgin')")
    public String assignRole(@PathVariable("roleId") Long id,Map map){
        //map中存放的是两个下拉列选集合
        List<Map<String, Object>> zNodes = permissionService.findPermissionByRoleId(id);
        map.put("zNodes", JSON.toJSONString(zNodes));
        map.put("roleId", id);
        return PAGE_ASSIGN_ROLE;

    }


    @PreAuthorize("hasAuthority('role.delete')")
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id) {
        roleService.delete(id);
        return ACTION_LIST;
    }

    @RequestMapping("/edit/{id}")
    @PreAuthorize("hasAuthority('role.edit')")
    public String edit(Model model, @PathVariable("id") Long id) {
        Role role = roleService.getById(id);
        model.addAttribute("role", role);
        return PAGE_EDIT;
    }

    @RequestMapping("/update")
    @PreAuthorize("hasAuthority('role.edit')")
    public String update(Role role, Map map, HttpServletRequest request) {
        roleService.update(role);
        return this.successPage("修改成功", request);
    }

    @RequestMapping("/save")
    @PreAuthorize("hasAuthority('role.create')")
    public String save(Role role, Map map, HttpServletRequest request) {//SpringMVC框架根据反射创建bean对象，并调用参数名称的set方法，将参数封装到bean对象中
        roleService.insert(role);
        return this.successPage("添加成功", request);
    }
    @PreAuthorize("hasAuthority('role.create')")
    @RequestMapping("/create")
    public String create() {

        return PAGE_CREATE;
    }

//    @RequestMapping
//    public String index(Model model) {
//        List<Role> list = roleService.findAll();
//
//        model.addAttribute("list", list);
//
//        return PAGE_INDEX;
//    }

    /**
     * 分页查询
     * 查询条件进行查询
     * roleName = ''
     * pageNum = 1  隐藏域
     * pageSize = 10 隐藏域
     *
     * @param model
     * @return
     */
    @RequestMapping
    //设置权限控制注解，在访问方法前需要校验用户权限
    @PreAuthorize("hasAuthority('role.show')")
    public String index(HttpServletRequest request, Model model) {
        Map<String, Object> filters = getFilters(request);
        //分页对象，将集合的数据，pageNum，pageSize，pages，total等
        PageInfo<Role> pageInfo = roleService.findPage(filters);
        model.addAttribute("page", pageInfo);
        model.addAttribute("filters", filters);
        return PAGE_INDEX;
    }


}
